package com.itheima.edu.info.manager.entry;

import com.itheima.edu.info.manager.domain.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.*;

public class Login {
    private static final Logger LOGGER = LoggerFactory.getLogger(Login.class);

    public static void main(String[] args) {
        start();

    }

    public final static void start() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            LOGGER.info("登录功能...........");
            System.out.println("请输入您的选择:1.登录    2.关闭");
            String select = sc.next();
            switch (select) {
                case "1":
                    try {
                        login();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case "2":
                    LOGGER.info("关闭页面.............");
                    System.exit(0);
                default:
                    System.out.println("您的输入有误,请重新输入");
            }
        }
    }

    public final static void login() throws IOException {


        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的管理员id:");
            String id = sc.next();
            System.out.println("请输入密码password:");
            String password = sc.next();
            String code = getCode();
            System.out.println("验证码为:" + code);
            System.out.println("请输入验证码:");
            String codeInput = sc.next();
            if (code.equalsIgnoreCase(codeInput)) {
                //建立连接
                Socket socket = new Socket("127.0.0.1", 10000);
                //发送数据
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write("[0]," + id + "," + password);
                bw.flush();
                socket.shutdownOutput();
                LOGGER.info("正在查询服务器,发出指令:[0]");
                //接收数据
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String s = br.readLine();
                LOGGER.info("接收到服务器响应返回是否登录成功" + s);
                socket.close();
                if (s.equals("true")) {
                    System.out.println("登陆成功");
                    ClientEntry clientEntry = new ClientEntry();
                    clientEntry.start();
                } else {
                    System.out.println("用户id或用户密码错误");
                }
            } else {
                System.out.println("验证码错误");
            }

        }
    }

    //得到验证码
    public static String getCode() {
        StringBuilder code = new StringBuilder();
        ArrayList list = new ArrayList(List.of("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"));
        for (char j = 'A'; j <= 'Z'; j++) {
            list.add(j);
        }
        for (char i = 'a'; i <= 'z'; i++) {
            list.add(i);
        }
        Collections.shuffle(list);
        for (int i = 0; i < 4; i++) {
            code.append(list.get(i));
        }
        return code.toString();
    }
}
